package com.metalrain.alm.enums;

import com.philips.lighting.model.PHBridge;
import com.philips.lighting.model.PHLight;
import com.philips.lighting.model.PHLightState;

/**
 * Created by administrator on 2014-06-13.
 */
public enum Modes {
    rainbow, candle;

    private PHLightState lightState = new PHLightState();
    int hue = (int) (Math.random()*65535);
    double speed = 20*(Math.random()-0.5);
    long lastTimeMillis = 0l;

    public void process(PHLight light, PHBridge bridge) {
        switch (this) {
            case rainbow:
                doRainbowLights(light, bridge);
                break;
            case candle:
                doCandleLight(light,bridge);
                break;
        }


    }

    private void doRainbowLights(PHLight light, PHBridge bridge) {
        if (lastTimeMillis == 0l) {     //Precondition that we are timing already, skip first iteration and start timing
            lastTimeMillis = System.currentTimeMillis();
            return;
        }

        long deltaTime = System.currentTimeMillis() - lastTimeMillis;
        lastTimeMillis += deltaTime;
        hue += ((double) deltaTime * speed);
        if (hue > 65535) hue = 0;
        if (hue < 0) hue = 65535;

        lightState.setColorMode(PHLight.PHLightColorMode.COLORMODE_HUE_SATURATION);
        lightState.setHue((int) hue);
        lightState.setSaturation(128);
        bridge.updateLightState(light, lightState);
    }

    private void doCandleLight(PHLight light, PHBridge bridge) {
        if (lastTimeMillis == 0l) {     //Precondition that we are timing already, skip first iteration and start timing
            lastTimeMillis = System.currentTimeMillis();
            return;
        }

        long deltaTime = System.currentTimeMillis() - lastTimeMillis;
        lastTimeMillis += deltaTime;
        hue += ((double) deltaTime * speed);
        if (hue > 65535) hue = 0;
        if (hue < 0) hue = 65535;

        lightState.setColorMode(PHLight.PHLightColorMode.COLORMODE_CT);
        lightState.setCt(5000);
        //lightState.setSaturation((int) (230 + (Math.random() * 15)));
        lightState.setBrightness(150+(int)(Math.random()*70 ));
        bridge.updateLightState(light, lightState);
    }


}
